package com.spremoteservice.android;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.client.ClientProtocolException;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;

public class ActivitySettings extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    setContentView(R.layout.activity_settings);
	    
	    ToggleButton syncSwitch = (ToggleButton)findViewById(R.id.toggleButton1);
		EditText edtAppId = (EditText)findViewById(R.id.edt_app_id);
		edtAppId.setEnabled(syncSwitch.isChecked());

	    syncSwitch.setOnClickListener(new OnClickListener()
	    {
	    	@Override
	        public void onClick(View view){
	    		ToggleButton syncSwitch = (ToggleButton) findViewById(R.id.toggleButton1);
	    		EditText edtAppId = (EditText)findViewById(R.id.edt_app_id);
	    		Boolean b = syncSwitch.isChecked();
	    		if(b){
	    			edtAppId.setEnabled(true);
	    		}else{
	    			edtAppId.setEnabled(false);
	    		}
	            //Toast.makeText(getApplicationContext(), "Click!" + b.toString(), Toast.LENGTH_SHORT).show();
	        }
	    });
	}
	
	public void ok(View view) throws ClientProtocolException, IOException, ParserConfigurationException, SAXException{
		CallAsyncRemote task = new CallAsyncRemote();
		EditText edtAppId = (EditText) findViewById(R.id.edt_app_id);
		String appId = edtAppId.getText().toString();
		task.execute(new String[]{appId});
		try {
			task.get(120000, TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Boolean b = task.GetResult();
		if(b){
			Toast.makeText(getApplicationContext(), "Connected to " + appId, Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(getApplicationContext(), "Cannot connect to " + appId, Toast.LENGTH_SHORT).show();
		}
	}

	public void cancel(View view){
		
	}
	public void pageDown(View view){
		
	}


}

class CallAsyncRemote extends AsyncTask<String, Void, Boolean>{
	
	private Boolean result;
	
	public Boolean GetResult(){
		return result;
	}
	
	@Override
	protected Boolean doInBackground(String... arg0) {
		String appId = arg0[0];
		result = false;
		Connector connector = Connector.GetInstance();
		try {
			if(connector.ConnectTo(appId)){
				result = true;
			}else{
				result = false;			
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;	
	}
	
}
